/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-11-06
 * Time:22:40
 */

/**
 * [0,1,2,3,4,5]
 * 3
 * 4
 * [1000000,1000001,1000002]
 * [0,1,2,1000000,1000001,1000002,5]
 */

import java.util.List;

/**
 * 力扣1669.合并两个链表
 */
public class mergeInBetween {
    public static void main(String[] args) {
//        ListNode list1 = new ListNode(0);
//        ListNode l1 = list1;
//        list1.next = new ListNode(1);
//        list1 = list1.next;
//        list1.next = new ListNode(2);
//        list1 = list1.next;
//        list1.next = new ListNode(3);
//        list1 = list1.next;
//        list1.next = new ListNode(4);
//        list1 = list1.next;
//        list1.next = new ListNode(5);
//
//        ListNode list2 = new ListNode(100000);
//        ListNode l2 = list2;
//        list2.next = new ListNode(100001);
//        list2 = list2.next;
//        list2.next = new ListNode(100002);
        String s;
        //System.out.println(s);


        //mergeInBetween(l1,4,6,l2);
    }
    public static ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        ListNode p1 = list1;
        ListNode p2 = list2;
        ListNode pre = null;
        ListNode suf = null;
        int pos = 0;
        while (p1 != null) {
            // 找到a索引前的节点pre
            if (pos == a - 1) {
                pre = p1;
            }
            // 找到b索引后的节点suf
            if (pos == b) {
                suf = p1.next;
            }
            p1 = p1.next;
            pos++;
        }
        // 将list2拼接在pre后
        pre.next = list2;
        // 找到list2最后一个节点
        while (p2.next != null) {
            p2 = p2.next;
        }
        // 将suf拼接在list2最后一个节点后
        p2.next = suf;
        return list1;

    }
}


